#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include "Mutex.hpp"
#include "Cond.hpp"
#include "Thread.hpp"
#include "Log.hpp"

namespace ThreadPoolModule
{
    using namespace LockModule;
    using namespace CondModule;
    using namespace ThreadModule;
    using namespace LogMudule;

    // debug
    void DefaultTest()
    {
        while (true)
        {
            LOG(LogLevel::DEBUG) << "测试";
            sleep(1);
        }
    }
    using thread_t = std::shared_ptr<Thread>; // 线程对象

    const static int defaultnum = 5;

    template <typename T>
    class ThreadPool
    {
    private:
        bool IsEmpty() { return _taskq.empty(); }
        void HandlerTask(std::string name)
        {
            LOG(LogLevel::INFO) << "线程:" << name << ", 进入HandlerTask的逻辑";
            while (true)
            {
                // 1.拿任务
                T t;
                {
                    LockGuard lockguard(_lock);
                    // 队列不为空 线程池正常运行 或者 队列为空 线程池退出，则循环退出
                    while (IsEmpty() && _isrunning)
                    {
                        _wait_num++;
                        _cond.Wait(_lock);
                        _wait_num--;
                    }
                    // 队列为空 线程池也退出，直接退出循环
                    if(IsEmpty() && !_isrunning)
                        break;
                    t = _taskq.front();
                    _taskq.pop();
                }
                // 2.处理任务
                t(name); 
            }
            LOG(LogLevel::INFO) << "线程: " << name << " 退出";
        }
    public:
        ThreadPool(int num = defaultnum)
            : _num(num)
            , _wait_num(0)
            , _isrunning(false)
        {
            // 创建线程对象
            for (int i = 0; i < _num; i++)
            {
                _threads.push_back(std::make_shared<Thread>([this](std::string name){ HandlerTask(name); }));
                LOG(LogLevel::INFO) << "构建线程" << _threads.back()->Name() << "对象 ... 成功";
            }
        }
        void Equeue(T&& in)
        {
            LockGuard lockguard(_lock);
            if(!_isrunning) return;
            _taskq.push(std::move(in));
            if(_wait_num > 0)
                _cond.Notify();
        }
        void Start()
        {
            // 防止线程被重复启动
            if(_isrunning)  return;
            _isrunning = true;
            for (auto &thread_ptr : _threads)
            {
                thread_ptr->Start();
                LOG(LogLevel::INFO) << "启动线程" << thread_ptr->Name() << " ... 成功";
            }
        }
        void Wait()
        {
            for (auto &thread_ptr : _threads)
            {
                thread_ptr->Join();
                LOG(LogLevel::INFO) << "回收线程" << thread_ptr->Name() << " ... 成功";
            }
        }
        void Stop()
        {
            LockGuard lockguard(_lock);
            // 只有线程是运行状态，才退出
            if(_isrunning)
            {
                // 3. 不在入任务了
                _isrunning = false;
                // 1. 让线程自己退出(要唤醒) &&  2. 历史的任务被处理完了
                if(_wait_num > 0)
                    _cond.NotifyAll();
            }
        }
        ~ThreadPool()
        {
        }
    private:
        std::vector<thread_t> _threads; // 管理线程实体
        int _num;                       // 线程个数
        int _wait_num;
        std::queue<T> _taskq;           // 任务队列

        Mutex _lock;
        Cond _cond;

        bool _isrunning;
    };
}